/*
 * @Author: your name
 * @Date: 2021-09-15 10:43:13
 * @LastEditTime: 2021-09-16 13:33:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \media_system\db\models\user.js
 */
const Redis = require("ioredis");
const redis = new Redis();
var moment = require('moment');
module.exports = (sequelize, DataTypes) => {
  const Food = sequelize.define('food', {
    foodId: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
      comment: '食物ID'
    },
    foodName: {
      type: DataTypes.STRING,
      comment: '食物名称'
    },
    protein: {
      type: DataTypes.DOUBLE,
      comment: '蛋白质(g)'
    },
    fat: {
      type: DataTypes.DOUBLE,
      comment: '脂肪(g)'
    },
    carbohydrate: {
      type: DataTypes.DOUBLE,
      comment: '碳水(g)'
    },
    caloric: {
      type: DataTypes.DOUBLE,
      comment: '热量(kcal)'
    },
    fiber: {
      type: DataTypes.DOUBLE,
      comment: '不溶性膳食纤维(g)'
    },
    cholesterol: {
      type: DataTypes.DOUBLE,
      comment: '胆固醇(mg)'
    },
    vitamina: {
      type: DataTypes.DOUBLE,
      comment: '维生素A(ugRAE)'
    },
    vitaminc: {
      type: DataTypes.DOUBLE,
      comment: '维生素C(mg)'
    },
    calcium: {
      type: DataTypes.DOUBLE,
      comment: '钙(mg)'
    },
    potassium: {
      type: DataTypes.DOUBLE,
      comment: '钾(mg)'
    },
    sodium: {
      type: DataTypes.DOUBLE,
      comment: '钠(mg)'
    },
    givalue: {
      type: DataTypes.STRING,
      comment: '食物血糖生成指数'
    },
    unit: {
      //0（固体） 1（液体)
      type: DataTypes.INTEGER,
      comment: "食物状态",
    },
    type: {
      //食物类型（0 其他 1谷类及制品 2薯类、淀粉及制品 3干豆类及制品 4蔬菜类及制品 5菌藻类 6水果类及制品 7坚果、种子类 8植物油 9畜肉类及制品 10禽肉类及制品 11乳类及制品 12蛋类及制品 13鱼虾蟹贝类 14油脂 15婴幼儿食品 16小吃、甜饼 17速食食品 18饮料类 19含酒精饮料 20糖、果脯和蜜饯、蜂蜜类 21调味品类）
      type: DataTypes.INTEGER,
      comment: "食物类型",
    },
    logoImg: {
      type: DataTypes.TEXT,
      comment: '食物列表logo图片'
    },
    avator: {
      type: DataTypes.STRING,
      comment: '食物封面图片'
    },
    desc: {
      type: DataTypes.TEXT,
      comment: '食物描述'
    },
    createdAt: {
      type: DataTypes.DATE,
      get() {
        const createdAt = this.getDataValue('createdAt')
        return moment(createdAt).format('YYYY-MM-DD HH:mm:ss');
      },
      set(createdAt) {
        return this.setDataValue('createdAt', createdAt)
      }
    },
    updatedAt: {
      type: DataTypes.DATE,
      get() {
        const updatedAt = this.getDataValue('updatedAt')
        return moment(updatedAt).format('YYYY-MM-DD HH:mm:ss');
      },
      set(updatedAt) {
        return this.setDataValue('updatedAt', updatedAt)
      }
    }
  },
    {
      tableName: 'foods',
      timestamps: true,
      hooks: {
        afterUpdate: async (instance, options) => {
          const oldfood = instance._previousDataValues
          const newfood = instance.dataValues
          if(!oldfood.unit){
            oldfood.unit=0
          }
          // console.log(instance.dataValues,instance._previousDataValues,redis.sismember('foodInfos',oldfood.foodName+"_"+oldfood.foodId+"_"+oldfood.unit))
          const isexist = await redis.sismember('foodInfos',oldfood.foodName+"#"+oldfood.foodId+"_"+oldfood.unit)
          if(isexist==1){
            //表示该食物存在redis中，即可更新redis(删了再加)
            // console.log(oldfood.foodName+"_"+oldfood.foodId+"_"+oldfood.unit,newfood.foodName+"_"+newfood.foodId+"_"+newfood.unit)
            await redis.srem('foodInfos',oldfood.foodName+"#"+oldfood.foodId+"_"+oldfood.unit)
            await redis.sadd('foodInfos',newfood.foodName+"#"+newfood.foodId+"_"+newfood.unit)
          }
        },
        afterDestroy:async (instance, options)=>{
          console.log(instance)
          const oldfood = instance._previousDataValues
          const newfood = instance.dataValues
          if(!oldfood.unit){
            oldfood.unit=0
          }
          const isexist = await redis.sismember('foodInfos',oldfood.foodName+"#"+oldfood.foodId+"_"+oldfood.unit)
          if(isexist==1){
            //表示该食物存在redis中，即可删除redis
            // console.log(oldfood.foodName+"#"+oldfood.foodId+"_"+oldfood.unit,newfood.foodName+"_"+newfood.foodId+"_"+newfood.unit)
            await redis.srem('foodInfos',oldfood.foodName+"#"+oldfood.foodId+"_"+oldfood.unit)
          }
        }
      },
    });

  return Food
}





